<template>
  <div id="myList" class="myList">
    <ul>
      <li v-for="item in list" :key="item.gid">
        <el-popover placement="top">
          <p>确定删除吗？</p>
          <div style="text-align: right; margin: 10px 0 0">
            <el-button type="primary" size="mini" @click="deleteCollect(item.gid)">确定</el-button>
          </div>
          <i class="el-icon-close delete" slot="reference" v-show="isDelete"></i>
        </el-popover>
        <router-link :to="{ path: '/goods/details', query: {gid:item.gid} }">
          <el-tag type="danger" size="mini" v-if="item.activity.name!==null&&item.activity.name!==undefined&&item.activity.name!==''" >
            {{item.activity.name}}
          </el-tag>
          <img :src="baseURL+'data/picture/'+item.picture.url" alt/>
          <h2>{{ item.name }}</h2>
          <h3>{{ item.merchant.name }}</h3>
          <p>
            <span>{{ item.price }}元</span>
          </p>
          <span  class="lowLeft">浏览量:{{item.view}}</span><span class="lowRight" style="float:right">销量:{{item.sale}}</span>
        </router-link>
      </li>
      <li v-show="isMore && list.length>=1" id="more">
        <router-link :to="{ path: '/goods', query: {caid:categoryID} }">
          浏览更多
          <i class="el-icon-d-arrow-right"></i>
        </router-link>
      </li>
      <li v-show="isActivity && list.length>=1" id="more">
        <router-link :to="{ path: '/goods', query: {aid:activityID} }">
          浏览更多
          <i class="el-icon-d-arrow-right"></i>
        </router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "MallList",
  // list为父组件传过来的商品列表
  // isMore为是否显示“浏览更多”
  props: ["list", "isMore", "isDelete","collectList","isActivity"],
  data() {
    return {};
  },
  created() {
  },
  computed: {
    /**
     * @后端资源路由
     * */
    baseURL() {
      return this.$store.state.baseURL;
    },
    activityID: function () {
      let activityID = [];
      if (this.list !== "") {
        for (let i = 0; i < this.list.length; i++) {
          const id = this.list[i].activity.aid;
          if (!activityID.includes(id)) {
            activityID.push(id);
          }
        }
      }
      //console.log(categoryID)
      return activityID.length===1?activityID[0]:0;
    },
    // 通过list获取当前显示的商品的分类ID，用于“浏览更多”链接的参数
    categoryID: function () {
      let categoryID = [];
      if (this.list !== "") {
        for (let i = 0; i < this.list.length; i++) {
          const id = this.list[i].card.caid;
          if (!categoryID.includes(id)) {
            categoryID.push(id);
          }
        }
      }
      //console.log(categoryID)
      return categoryID.length===1?categoryID[0]:0;
    }
  },
  methods: {
    getCoid(gid){
      for (let i = 0; i < this.collectList.length; i++) {
        //console.log(this.collectList[i].good.gid+"\n"+gid)
        if(this.collectList[i].good.gid===gid){
          return this.collectList[i].coid;
        }
      }
      return -1;
    },
    deleteCollect(gid) {
      //console.log(this.collectList)
      //console.log(this.list)
      let coid=this.getCoid(gid);
      this.$axios
          .post(this.$store.state.baseURL + 'client/' + this.$store.state.controller.Collect +
              '/'+this.$store.state.mappings.save+'?' + 'OperatorId=' + this.$store.getters.getUser.cid,
              {
                coid: coid,
                customer: {cid:this.$store.getters.getUser.cid},
                good:{gid:gid},
                state:0
          })
          .then(res => {
            switch (res.data) {
              case 0:
                // 提示删除失败信息
                this.notifyError("失败==>SQL调用失败");
                break;
              case -1:
                // 提示删除失败信息
                this.notifyError("失败==>服务熔断");
                break;
              default:
                // 删除成功
                // 删除删除列表中的该商品信息
                for (let i = 0; i < this.list.length; i++) {
                  const temp = this.list[i];
                  if (temp.gid === gid) {
                    this.list.splice(i, 1);
                  }
                }
                // 提示删除成功信息
                this.notifySucceed("成功==>取消收藏");
                break;
            }
          })
          .catch(err => {
            return Promise.reject(err);
          });
    }
  }
}
</script>

<style scoped>
.myList ul li {
  z-index: 1;
  float: left;
  width: 234px;
  height: 300px;
  padding: 10px 0;
  margin: 0 0 14.5px 13.7px;
  background-color: white;
  -webkit-transition: all 0.2s linear;
  transition: all 0.2s linear;
  position: relative;
}

.myList ul li:hover {
  z-index: 2;
  -webkit-box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  -webkit-transform: translate3d(0, -2px, 0);
  transform: translate3d(0, -2px, 0);
}

.myList ul li img {
  display: block;
  width: 160px;
  height: 160px;
  background: url(/assets/imgs/placeholder.png) no-repeat 50%;
  margin: 0 auto;
}

.myList ul li h2 {
  margin: 25px 10px 0;
  font-size: 14px;
  font-weight: 400;
  color: #333;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.myList ul li h3 {
  margin: 5px 10px;
  height: 18px;
  font-size: 12px;
  font-weight: 400;
  color: #b0b0b0;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.myList ul li p {
  margin: 10px 10px 10px;
  text-align: center;
  color: #ff6700;
}

.myList ul li p .del {
  margin-left: 0.5em;
  color: #b0b0b0;
  text-decoration: line-through;
}

.myList #more {
  text-align: center;
  line-height: 280px;
}

.myList #more a {
  font-size: 18px;
  color: #333;
}

.myList #more a:hover {
  color: #ff6700;
}

.myList ul li .delete {
  position: absolute;
  top: 10px;
  right: 10px;
  display: none;
}

.myList ul li:hover .delete {
  display: block
}

.myList ul li .delete:hover {
  color: #ff6700;
}
.lowLeft{
  bottom:0;
  position:absolute;
  left:0;
}
.lowRight{
  bottom:0;
  position:absolute;
  right:0;
}
</style>